package com.skyable.device.interceptor;

import com.skyable.common.constants.param.CommonParams;
import com.skyable.device.config.CloudApplicationContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author GaoYuan
 * @date 2020/5/26 20:02
 * @desc feign调用时 新增请求头信息
 */
@Component
public class FeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate requestTemplate){
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        if(servletRequestAttributes != null) {
            HttpServletRequest request = servletRequestAttributes.getRequest();
            requestTemplate.header(CommonParams.HEADER_USER_NAME, request.getHeader(CommonParams.HEADER_USER_NAME));
            requestTemplate.header(CommonParams.HEADER_USER_DOMAIN_ID, request.getHeader(CommonParams.HEADER_USER_DOMAIN_ID));
        }
    }
}
